package com.weirn.spring.commons.utils;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.io.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Created by runing.wei on 2018/6/29.
 */
public class ReadTxt {

    private String path = "";

    @Test
    public void readFile(){

        String path = "/Users/yp-tc-2563/Downloads/merchantno_hashcode.txt";

        List<MerchantNoHashCode> lists = new LinkedList<MerchantNoHashCode> (  );
        File file = new File (path);
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        if(file.isFile ()){
            try {

                fileReader = new FileReader ( file );
                bufferedReader = new BufferedReader ( fileReader );

                try {
                    String dataLine = null;
                    while ((dataLine = bufferedReader.readLine ()) != null){
                        String[] datas = dataLine.split ( "," );
                        MerchantNoHashCode merchantNoHashCode = new MerchantNoHashCode ();
                        merchantNoHashCode.setMerchantNo ( datas[0] );
                        merchantNoHashCode.setDataCount ( datas[1] );
                        merchantNoHashCode.setHashcode ( datas.hashCode ()%4L );
                        lists.add ( merchantNoHashCode );
                    }

                } catch ( IOException e ) {
                    e.printStackTrace ();
                }
            } catch ( FileNotFoundException e ) {
                e.printStackTrace ();
            }finally {
                if(bufferedReader !=null){
                    try {
                        bufferedReader.close ();
                    } catch ( IOException e ) {
                        e.printStackTrace ();
                    }
                }
                if(fileReader != null){
                    try {
                        fileReader.close ();
                    } catch ( IOException e ) {
                        e.printStackTrace ();
                    }
                }
            }
        }else{
            System.out.println ("");
        }

        Set<String> merchantNos = new HashSet<String> (  );


        for ( int i = 0; i < 4; i++ ) {
            Long countData = 0L;
            for ( MerchantNoHashCode merchantNoHashCode:lists ) {
                if(merchantNoHashCode.getHashcode () == i){
                    merchantNos.add ( merchantNoHashCode.getMerchantNo () );
                    countData = countData + (merchantNoHashCode.getDataCount ().equals ( "" ) ? 0L :  Long.valueOf (merchantNoHashCode.getDataCount () ));
                }
            }
            System.out.println ( "散列值为"+i+ ",数据量" + countData);

        }

        for ( MerchantNoHashCode merchantNoHashCode:lists ) {

            System.out.println ( JSON.toJSONString ( merchantNoHashCode ));

        }


    }


    class MerchantNoHashCode{
        private String merchantNo;

        private String dataCount;

        private Long hashcode;

        public MerchantNoHashCode() {
        }

        public String getMerchantNo() {
            return merchantNo;
        }

        public void setMerchantNo(String merchantNo) {
            this.merchantNo = merchantNo;
        }

        public String getDataCount() {
            return dataCount;
        }

        public void setDataCount(String dataCount) {
            this.dataCount = dataCount;
        }

        public Long getHashcode() {
            return hashcode;
        }

        public void setHashcode(Long hashcode) {
            this.hashcode = hashcode;
        }
    }


}
